package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BossJobDao;
import com.qc.entity.BossJobEntity;

@Repository("bossJobDao")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class BossJobDaoImpl implements BossJobDao{

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListBossJobCount(String name) {
        String sql = "select count(*) from t_boss_job where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List<BossJobEntity> getAllBossJob(String name, int pageNo,int pageSize) {
        
        int beginRow = (pageNo - 1)* pageSize;
        
        String sql = "select job_id,name from t_boss_job where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BossJobEntity.class));
    }

    @Override
    public BossJobEntity getBossJobId(String jobId) {
        String sql = "select job_id,name from t_boss_job where 1 = 1  and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(jobId) && !"undefined".equals(jobId) ) {
            sql += " and job_id = '"+jobId+"' ";
        }
        return (BossJobEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BossJobEntity.class));
    }

    @Override
    public int saveBossJob(BossJobEntity bossJob) {
        
        String sql = "insert into t_boss_job(name) values(?)";
        return jdbcTemplate.update(sql,new Object[]{bossJob.getName()});
    }

    @Override
    public int deleteBossJobId(long id) {
        
        String sql = "update t_boss_job set if_del = '1' where job_id = " +id;
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updateBossJobId(BossJobEntity bossJob) {

        String sql = "update t_boss_job set name = ? where job_id = ? ";
        return jdbcTemplate.update(sql,new Object[]{bossJob.getName(),bossJob.getJobId()});
    }

	@Override
	public List<BossJobEntity> getAllBossJob() {
		String sql = "select * from qc.t_boss_job where if_del = 0";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BossJobEntity.class));
	}

    

}
